package com.zhiyu.common;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public abstract class BaseService<M extends BaseMapper<T>, T extends BaseEntity> {
    @Autowired
    protected M mapper;

    public T get(Integer id) {
        return mapper.get(id);
    }

    public T get(T entity) {
        return mapper.get(entity);
    }

    public List<T> findList(BaseEntity entity) {
        return mapper.findList(entity);
    }

    public PageInfo<T> findPage(BaseEntity entity) {
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<T> list = mapper.findList(entity);
        return new PageInfo<>(list);
    }

    public PageInfo<T> findPage(BaseEntity entity, int navigatePages) {
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<T> list = mapper.findList(entity);
        return new PageInfo<>(list, navigatePages);
    }

    public int insert(T entity){ return mapper.insert(entity); }

    public int update(T entity) {
        return mapper.update(entity);
    }

    public int delete(T entity) {
        return mapper.delete(entity);
    }
}
